CREATE FUNCTION AddMaHD(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='HD' SET @iMaso= (SELECT MAX(RIGHT(MaHD,8)) FROM HoaDon)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'00000001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '0000000'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,8)
	END
RETURN @vMaso 
END
go
ALTER TABLE HoaDon ADD CONSTRAINT def_HoaDon DEFAULT dbo.AddMaHD('HD') FOR MaHD

-----------------------------------------------------------------------
CREATE FUNCTION AddMaKH(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='KH' SET @iMaso= (SELECT MAX(RIGHT(MaKH,8)) FROM KhachHang)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'00000001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '0000000'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,8)
	END
RETURN @vMaso 
END
go
ALTER TABLE KhachHang ADD CONSTRAINT def_KhachHang DEFAULT dbo.AddMaKH('KH') FOR MaKH

-----------------------------------------------------------------------
CREATE FUNCTION AddMaMK(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='MK' SET @iMaso= (SELECT MAX(RIGHT(MaKho,3)) FROM Kho)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '00'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,3)
	END
RETURN @vMaso 
END
go
ALTER TABLE Kho ADD CONSTRAINT def_Kho DEFAULT dbo.AddMaMK('MK') FOR MaKho

-----------------------------------------------------------------------
CREATE FUNCTION AddMaQH(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='QH' SET @iMaso= (SELECT MAX(RIGHT(MaQuay,3)) FROM Quay)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'0001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '000'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,3)
	END
RETURN @vMaso 
END
go
ALTER TABLE Quay ADD CONSTRAINT def_Quay DEFAULT dbo.AddMaQH('QH') FOR MaQuay

-----------------------------------------------------------------------
CREATE FUNCTION AddMaNV(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='NV' SET @iMaso= (SELECT MAX(RIGHT(MaNV,8)) FROM NhanVien)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'00000001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '0000000'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,8)
	END
RETURN @vMaso 
END
go
ALTER TABLE NhanVien ADD CONSTRAINT def_NhanVien DEFAULT dbo.AddMaNV('NV') FOR MaNV

-----------------------------------------------------------------------
CREATE FUNCTION AddMaMQ(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='MQ' SET @iMaso= (SELECT MAX(RIGHT(MaQuyen,3)) FROM Quyen)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '00'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,3)
	END
RETURN @vMaso 
END
go
ALTER TABLE Quyen ADD CONSTRAINT def_Quyen DEFAULT dbo.AddMaMQ('MQ') FOR MaQuyen

-----------------------------------------------------------------------
CREATE FUNCTION AddMaSP(@makt char(2))
RETURNS varchar(10)
AS
BEGIN 
	DECLARE @iMaso int 
  	DECLARE @vMaso varchar(10) 
  	IF @makt='SP' SET @iMaso= (SELECT MAX(RIGHT(MaSP,8)) FROM SanPham)  	
	IF (@iMaso IS NULL) SET @vMaso= @makt+ CONVERT(varchar(10),'00000001')
	ELSE
	BEGIN
		SET @iMaso= @iMaso+1
	    	SET @vMaso= '0000000'+ CONVERT(varchar,@iMaso) 
	    	SET @vMaso= @makt+ RIGHT(@vMaso,8)
	END
RETURN @vMaso 
END
go
ALTER TABLE SanPham ADD CONSTRAINT def_SanPham DEFAULT dbo.AddMaSP('SP') FOR MaSP

-----------------------------------------------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Search
@MaNV varchar(10)
as
begin
	select * from NhanVien where MaNV =@MaNV 
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Delete
@MaNV varchar(10)
as
begin
	delete from NhanVien where MaNV =@MaNV 
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------
create procedure NhanVien_Load
as
begin
	select * from NhanVien
end
------------------------------
go
------------------------------